#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/2 15:45
# @Author  : Swper
# @Site    : 
# @File    : forms.py
# @Software: PyCharm
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField,TextAreaField,validators,SelectMultipleField,SelectField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired,ValidationError
from app.models import User,Arctype

class LoginForm(FlaskForm):
    """管理员登陆表单"""
    name = StringField(
        label="用户名",
        validators=[DataRequired("请输入用户名！")],
        description="用户名",
        render_kw={"class":"form-control",
                   "placeholder":"请输入用户名",
                    "required":"required"
                   }
    )

    pwd = PasswordField(
        label="密码",
        validators=[DataRequired("请输入密码！")],
        description="密码",
        render_kw={
            "class":"form-control",
            "placeholder":"请输入密码",
            "required":"required"
        }
    )

    submit = SubmitField(
        "登陆",
        render_kw={
            "class":"btn btn-primary btn-block",
        }
    )
    # 验证账号和密码
    def validate_account(self,field):
        name = field.data
        user = User.query.filter_by(name=name).count()
        if user == 0:
            raise ValidationError("账号不存在！")



#系统设置
class SettingsForm(FlaskForm):
    weburl = StringField(
        label="网站域名",
        validators=[DataRequired("请输入域名！")],
        description="域名",
        render_kw={"class":"form-control",
                   "placeholder":"请输入域名",
                   }
    )
    webname = StringField(
        label="站点名称",
        validators=[DataRequired("请输入站点名称！")],
        description="站点名称",
        render_kw={"class":"form-control",
                   "placeholder":"请输入站点名称",
                   }
    )
    record = StringField(
        label="网站备案号",
        validators=[DataRequired("请输入网站备案号！")],
        description="网站备案号",
        render_kw={"class":"form-control",
                   "placeholder":"请输入网站备案号",
                   }
    )
    describe = TextAreaField(
        label="网站描述",
        validators=[DataRequired("请输入网站描述！")],
        description="网站描述",
        render_kw={"class":"form-control",
                   "placeholder":"请输入网站描述",
                   }
    )
    keywords = StringField(
        label="SEO关键字",
        validators=[DataRequired("请输入关键字，逗号隔开！")],
        description="SEO关键字",
        render_kw={"class":"form-control",
                   "placeholder":"请输入关键字",
                   }
    )
    submit = SubmitField(
        label="确认",
        render_kw={
            "type": "button",
            "class": "btn btn-primary js-edit",
            "id": "myModalLabel",
        }
    )


    rollbak = SubmitField(
        "重置",
        render_kw={
            "type": "button",
            "class": "btn btn-primary",
        }
    )


#系统设置
# class SettingsEditForm(FlaskForm):
#     weburl = StringField(
#         label="网站域名",
#     validators=[DataRequired("请输入域名！")],
#         description="域名",
#         render_kw={"class":"form-control",
#                    "disabled":"disabled",
#                    }
#     )
#     webname = StringField(
#         label="站点名称",
#         validators=[DataRequired("请输入站点名称！")],
#         description="站点名称",
#         render_kw={"class":"form-control",
#                    "disabled": "disabled",
#                    }
#     )
#     record = StringField(
#         label="网站备案号",
#         validators=[DataRequired("请输入网站备案号！")],
#         description="网站备案号",
#         render_kw={"class":"form-control",
#                    "disabled": "disabled",
#                    }
#     )
#     describe = TextAreaField(
#         label="网站描述",
#         validators=[DataRequired("请输入网站描述！")],
#         description="网站描述",
#         render_kw={"class":"form-control",
#                    "disabled": "disabled",
#                    }
#     )
#     keywords = StringField(
#         label="SEO关键字",
#         validators=[DataRequired("请输入关键字，逗号隔开！")],
#         description="SEO关键字",
#         render_kw={"class":"form-control",
#                    "disabled": "disabled",
#                    }
#     )
#     submit = StringField(
#         "编辑",
#         render_kw={
#             "type":"button",
#             "class":"btn btn-primary",
#             "id": "myModalLabel",
#
#         }
#     )



#添加栏目
class AddtypeForm(FlaskForm):
    tyname = StringField(
        label="栏目名称",
        validators=[DataRequired("请输入栏目名称！")],
        description="栏目名称",
        render_kw={"class":"form-control",
                   "placeholder":"请输入栏目名称",
                   }
    )

    # describe = TextAreaField(
    #     label="栏目描述",
    #     validators=[DataRequired("请输入栏目描述！")],
    #     description="栏目描述",
    #     render_kw={"class":"form-control",
    #                "placeholder":"请输入栏目描述",
    #                "required": "required"
    #                }
    # )
    keywords = StringField(
        default='',
        label="SEO关键字",
        validators=[DataRequired("请输入关键字，逗号隔开！")],
        description="SEO关键字",
        render_kw={"class":"form-control",
                   "placeholder":"本栏目关键字",
                   "required": "required"
                   }
    )
    submit = SubmitField(
        "确认",
        render_kw={
            # "type":"button",
            "class":"btn btn-primary",
            # "data-toggle" : "modal",
            # "data-target":"#modal-success",
            "id" :"myModalLabel"
        }
    )
#编辑栏目
class EditypeForm(FlaskForm):
    tyname = StringField(
        label="栏目名称",
        validators=[DataRequired("请输入栏目名称！")],
        description="栏目名称",
        render_kw={"class":"form-control",
                   "placeholder":"请输入栏目名称",
                   }
    )

    keywords = StringField(
        default='',
        label="SEO关键字",
        validators=[DataRequired("请输入关键字，逗号隔开！")],
        description="SEO关键字",
        render_kw={"class":"form-control",
                   "placeholder":"本栏目关键字",
                   "required": "required"
                   }
    )
    submit = SubmitField(
        "确认",
        render_kw={
            # "type":"button",
            "class":"btn btn-primary",
            # "data-toggle" : "modal",
            # "data-target":"#modal-success",
            # "id" :"myModalLabel"
        }
    )


#发布文章
class ArticleForm(FlaskForm):

    title = StringField(
        label="文章标题",
        validators=[DataRequired("请输入标题！")],
        description="文章标题",
        render_kw={"class":"form-control",
                   "placeholder":"文章标题",
                   }
    )

    keywords = StringField(
        default='',
        label="文章关键字",
        validators=[DataRequired("请输入关键字，逗号隔开！")],
        description="文章关键字",
        render_kw={"class":"form-control",
                   "placeholder":"关键字，逗号隔开",
                   }
    )
    typename = SelectField(
        label='所属栏目',
        # choices=[],
        description="栏目列表",
        render_kw={
            "style":"width:200px",
            "class":'form-control',
        }

    )


    content = TextAreaField(
        label='文章正文',
        validators=[DataRequired()],
        render_kw={
            "class":'ckeditor',
            "rows":"30",
            "cols":"40"
        }
    )

    submit = SubmitField(
        "确认",
        render_kw={
            "type":"button",
            "class":"btn btn-primary",
            "data-toggle" : "modal",
            "data-target":"#modal-success",
            "id" :"myModalLabel"
        }
    )
    #添加对于下拉列表的动态获取方法
    def __init__(self,*args,**kwargs):
        super(ArticleForm,self).__init__(*args,**kwargs)
        choices = [(v.id, v.tyname) for v in Arctype.query.all()]
        self.typename.choices = choices





#添加用户表单
class AddUserForm(FlaskForm):
    name = StringField(
        label="用户名",
        validators=[DataRequired("请输入用户名！")],
        description="用户名",
        render_kw={"class":"form-control",
                   "placeholder":"请输入用户名",
                   "required": "required"
                   }
    )

    email = StringField(
        default='',
        label="邮箱地址",
        validators=[DataRequired("请输入邮箱地址！")],
        description="邮箱地址",
        render_kw={"class":"form-control",
                   "placeholder":"邮箱地址",
                   "required": "required"
                   }
    )
    pwd = StringField(
        default='',
        label="密码",
        validators=[DataRequired("请输入密码！")],
        description="密码",
        render_kw={"class":"form-control",
                   "placeholder":"密码",
                   "required": "required"

                   }
    )
    status = SelectField(
        '状态',
        choices=[('0','正常'),('1','禁用')],
        # choices=[('cpp', 'C++'), ('py', 'Python'), ('text', 'Plain Text')],
        render_kw={
            "style": "width:80px",
            "class": 'form-control',
        }

    )
    submit = SubmitField(
        "确认",
        render_kw={
            "type":"button",
            "class":"btn btn-primary",
            # "data-toggle" : "modal",
            # "data-target":"#modal-success",
            "id" :"myModalLabel"
        }
    )


#添加友情连接
class AddFlinkForm(FlaskForm):
    webname = StringField(
        label="网站名",
        validators=[DataRequired("请输入网站名！")],
        description="网站名",
        render_kw={"class":"form-control",
                   "placeholder":"请输入网站名",
                   "required": "required"
                   }
    )
    weburl = StringField(
        label="网站URL",
        validators=[DataRequired("请输入网站URL！")],
        description="网站URL",
        render_kw={"class":"form-control",
                   "placeholder":"请输入网站URL",
                   "required": "required"
                   }
    )

    submit = SubmitField(
        "确认",
        render_kw={
            # "type":"button",
            "class":"btn btn-primary",
            # "data-toggle" : "modal",
            # "data-target":"#modal-success",
            "id" :"myModalLabel"
        }
    )


















